package com.xzx.email;

import javax.mail.Address;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

/**
 * 发送第一份邮件.
 * @author xinzhixuan
 * @version V1.0
 * @date 2017/9/20 21:48
 */
public class SendFirstEmail {

    public static void main(String[] args) throws MessagingException {

        Properties props = new Properties();
        props.setProperty("mail.transport.protocol", "smtp");
        props.setProperty("mail.smtp.auth", "true");
        Session session = Session.getInstance(props);//每次都创建一个新的session对象
//        Session.getDefaultInstance(props);//session对象不存在时创建一个，存在则不创建
        session.setDebug(true);

        Message message = new MimeMessage(session);//邮件内容
        message.setText("你好吗？");
        message.setFrom(new InternetAddress("xinzhixuan29@sina.com"));

        Transport transport = session.getTransport();//发送邮件
        //需要先连接到服务器
        transport.connect("smtp.sina.com", 25, "xinzhixuan29", "92803991nihao");
        transport.sendMessage(message, new Address[]{new InternetAddress("xinzhixuan29@163.com")});
        //最后需要关闭资源
        transport.close();
    }
}
